package com.ruoyi.project.ledger.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.ledger.domain.LedgerBusinessAccount;
import com.ruoyi.project.ledger.domain.LedgerFile;
import com.ruoyi.project.ledger.domain.LedgerFirefightingEquipment;
import com.ruoyi.project.ledger.mapper.LedgerFileMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.ledger.mapper.LedgerFirefightingFacilitiesMapper;
import com.ruoyi.project.ledger.domain.LedgerFirefightingFacilities;
import com.ruoyi.project.ledger.service.ILedgerFirefightingFacilitiesService;

/**
 * 消防设施Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-05-11
 */
@Service
public class LedgerFirefightingFacilitiesServiceImpl implements ILedgerFirefightingFacilitiesService 
{
    private static final Logger log = LoggerFactory.getLogger(LedgerFirefightingFacilitiesServiceImpl.class);

    @Autowired
    private LedgerFirefightingFacilitiesMapper ledgerFirefightingFacilitiesMapper;
    @Autowired
    private LedgerFileMapper ledgerFileMapper;

    /**
     * 查询消防设施
     * 
     * @param id 消防设施ID
     * @return 消防设施
     */
    @Override
    public LedgerFirefightingFacilities selectLedgerFirefightingFacilitiesById(Long id)
    {
        LedgerFirefightingFacilities facilities = ledgerFirefightingFacilitiesMapper.selectLedgerFirefightingFacilitiesById(id);
        if (facilities != null){
            // 获取图片
            LedgerFile ledgerFile = new LedgerFile();
            ledgerFile.setLedgerId(id);
            ledgerFile.setLedgerType("8");      // 消防设施
            List<Map<String, Object>> imgList = ledgerFileMapper.selectLedgerImgList(ledgerFile);
            facilities.setImgList(imgList);
        }
        return facilities;
    }

    /**
     * 查询消防设施列表
     * 
     * @param ledgerFirefightingFacilities 消防设施
     * @return 消防设施
     */
    @Override
    public List<LedgerFirefightingFacilities> selectLedgerFirefightingFacilitiesList(LedgerFirefightingFacilities ledgerFirefightingFacilities)
    {
        return ledgerFirefightingFacilitiesMapper.selectLedgerFirefightingFacilitiesList(ledgerFirefightingFacilities);
    }

    /**
     * 新增消防设施
     * 
     * @param ledgerFirefightingFacilities 消防设施
     * @return 结果
     */
    @Override
    public int insertLedgerFirefightingFacilities(LedgerFirefightingFacilities ledgerFirefightingFacilities)
    {
        ledgerFirefightingFacilities.setCreateTime(DateUtils.getNowDate());
        int i = ledgerFirefightingFacilitiesMapper.insertLedgerFirefightingFacilities(ledgerFirefightingFacilities);
        // 添加图片
        if(ledgerFirefightingFacilities.getImgs() != null && ledgerFirefightingFacilities.getImgs().length > 0){
            for(String path : ledgerFirefightingFacilities.getImgs()){
                LedgerFile ledgerFile = new LedgerFile();
                ledgerFile.setLedgerType("8");    // 消防设施
                ledgerFile.setLedgerId(ledgerFirefightingFacilities.getId());
                ledgerFile.setFilePath(path);
                ledgerFile.setCreateId(ledgerFirefightingFacilities.getCreateId());
                ledgerFile.setCreateTime(DateUtils.getNowDate());
                ledgerFileMapper.insertLedgerFile(ledgerFile);
            }
        }
        return i;
    }

    /**
     * 修改消防设施
     * 
     * @param ledgerFirefightingFacilities 消防设施
     * @return 结果
     */
    @Override
    public int updateLedgerFirefightingFacilities(LedgerFirefightingFacilities ledgerFirefightingFacilities)
    {
        ledgerFirefightingFacilities.setUpdateTime(DateUtils.getNowDate());
        int i = ledgerFirefightingFacilitiesMapper.updateLedgerFirefightingFacilities(ledgerFirefightingFacilities);
        // 删除台账图片
        LedgerFile ledgerFile = new LedgerFile();
        ledgerFile.setLedgerId(ledgerFirefightingFacilities.getId());
        ledgerFile.setLedgerType("8");      // 消防设施
        ledgerFileMapper.deleteLedgerImg(ledgerFile);
        if(ledgerFirefightingFacilities.getImgs() != null && ledgerFirefightingFacilities.getImgs().length > 0){
            for(String path : ledgerFirefightingFacilities.getImgs()){
                ledgerFile = new LedgerFile();
                ledgerFile.setLedgerType("8");    // 消防设施
                ledgerFile.setLedgerId(ledgerFirefightingFacilities.getId());
                ledgerFile.setFilePath(path);
                ledgerFile.setCreateId(ledgerFirefightingFacilities.getUpdateId());
                ledgerFile.setCreateTime(DateUtils.getNowDate());
                ledgerFileMapper.insertLedgerFile(ledgerFile);
            }
        }
        return i;
    }

    /**
     * 批量删除消防设施
     * 
     * @param ids 需要删除的消防设施ID
     * @return 结果
     */
    @Override
    public int deleteLedgerFirefightingFacilitiesByIds(Long[] ids)
    {
        int i = ledgerFirefightingFacilitiesMapper.deleteLedgerFirefightingFacilitiesByIds(ids);
        for(Long id : ids){
            // 删除台账图片
            LedgerFile ledgerFile = new LedgerFile();
            ledgerFile.setLedgerId(id);
            ledgerFile.setLedgerType("8");      // 消防设施
            ledgerFileMapper.deleteLedgerImg(ledgerFile);
        }
        return i;
    }

    /**
     * 删除消防设施信息
     * 
     * @param id 消防设施ID
     * @return 结果
     */
    @Override
    public int deleteLedgerFirefightingFacilitiesById(Long id)
    {
        int i = ledgerFirefightingFacilitiesMapper.deleteLedgerFirefightingFacilitiesById(id);
        // 删除台账图片
        LedgerFile ledgerFile = new LedgerFile();
        ledgerFile.setLedgerId(id);
        ledgerFile.setLedgerType("8");      // 消防设施
        ledgerFileMapper.deleteLedgerImg(ledgerFile);
        return i;
    }

    @Override
    public String importFacilities(List<LedgerFirefightingFacilities> facilitiesList, Boolean isUpdateSupport, String operName, Long stationId) {
        if (StringUtils.isNull(facilitiesList) || facilitiesList.size() == 0) {
            throw new CustomException("导入消防设施数据不能为空！");
        }
        if (stationId == null) {
            throw new CustomException("角色异常！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        int i=1;
        for (LedgerFirefightingFacilities facilities : facilitiesList) {
            i++;
            try {
                if(StringUtils.isBlank(facilities.getPosition())){
                    failureNum++;
                    failureMsg.append("<br/>第" + i + "行、消防设施位置不能为空");
                    continue;
                }
                facilities.setStationId(stationId);
                facilities.setCreateId(String.valueOf(stationId));
                LedgerFirefightingFacilities a = ledgerFirefightingFacilitiesMapper.selectLedgerFirefightingFacilities(facilities);
                if (StringUtils.isNull(a)) {
                    facilities.setCreateBy(operName);
                    facilities.setCreateTime(DateUtils.getNowDate());
                    this.insertLedgerFirefightingFacilities(facilities);
                    successNum++;
                    successMsg.append("<br/>第" + i + "行、消防设施 " + facilities.getPosition() + " 导入成功");
                } else if (isUpdateSupport) {
                    facilities.setUpdateBy(operName);
                    this.updateLedgerFirefightingFacilities(facilities);
                    successNum++;
                    successMsg.append("<br/>第" + i + "行、消防设施 " + facilities.getPosition() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>第" + i + "行、消防设施 " + facilities.getPosition() + " 已存在");
                }

            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>第" + i + "行、消防设施 " + facilities.getPosition() + " 导入失败：";
                failureMsg.append(msg + "该行数据信息有误，请仔细检查");
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public String checkFacilitiesUnique(LedgerFirefightingFacilities ledgerFirefightingFacilities) {
        LedgerFirefightingFacilities facilities = ledgerFirefightingFacilitiesMapper.selectLedgerFirefightingFacilities(ledgerFirefightingFacilities);
        if (facilities != null){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checkFacilitiesUnique2(LedgerFirefightingFacilities ledgerFirefightingFacilities) {
        Long id = StringUtils.isNull(ledgerFirefightingFacilities.getId()) ? -1L : ledgerFirefightingFacilities.getId();
        LedgerFirefightingFacilities facilities = ledgerFirefightingFacilitiesMapper.selectLedgerFirefightingFacilities(ledgerFirefightingFacilities);
        if (StringUtils.isNotNull(facilities) && facilities.getId().longValue() != id.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
